home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / compguid / clocks / kettle.sx
Encoding:
Text File  |  1996-05-21  |  2.5 KB  |  115 lines  |  [TEXT/ttxt]

  1. --<<<
  2.  
  3. -- Callback example
  4.  
  5.  
  6.  
  7. -- put on the kettle 
  8. -- the time the kettle takes to boil depends on 
  9. -- how many cups of water you are boiling
  10. -- while the kettle is heating up, it hums
  11. -- when it starts boiling, it whistles
  12.  
  13.  
  14. class Kettle (RootObject)
  15. instance variables
  16.     heatingTimePerCup:3
  17. end
  18.  
  19.  
  20. -- clock1 is the clock
  21. -- cups is the number of cups of water to heat
  22. -- t is the number of ticks from now to start the kettle
  23. -- onceOnly is true or false depending on whether you
  24. -- want the callback to cancel itself after its first invocation
  25.  
  26. method scheduleKettle self {class Kettle} clock1 cups t label onceOnly ->
  27. (
  28.     addTimeCallback clock1 putOnKettle self #(cups, clock1) \
  29.     (clock1.time + (t as time)) onceOnly
  30. )
  31.  
  32.  
  33.  
  34. method putOnKettle self {class Kettle} cups clock1 ->
  35. (
  36.     local t := clock1.time
  37.     format debug "Putting on the kettle at time %* \n" t @normal
  38.     local timePerCup := self.heatingTimePerCup
  39.     local timeToBoil := (timePerCup * cups) as time
  40.     
  41.     -- create a callback to schedule when the kettle boils
  42.     -- the callback will cancel itself when it has been invoked
  43.     local tb := addTimeCallBack clock1 startBoiling self \
  44.         #(clock1) (clock1.time + timeToBoil) true
  45.     
  46.     -- start heating the kettle
  47.     heatUpKettle self clock1
  48. )
  49.  
  50.  
  51. -- while the kettle is heating up, it hums
  52. -- use the prin method as the action for the 
  53. -- callback that does the humming
  54.  
  55. method heatUpKettle self {class Kettle}  clock1 ->
  56. (    
  57.     print "Starting to heat up the kettle "
  58.  
  59.     local cb := addPeriodicCallback clock1 \
  60.         prin "hum \n" #(@unadorned, debug) 1      
  61.         
  62.     -- give the callback a label so we can find it later
  63.     cb.label := "hum"    
  64. )
  65.  
  66.  
  67. -- when the kettle boils, it whistles
  68. method startBoiling self {class Kettle} clock1 ->
  69. (
  70.     local t := clock1.time
  71.     format debug "Starting to boil at time %*\n" t @normal
  72.     
  73.     -- cancel the callback that makes the hum play
  74.     local humCB := chooseOne clock1.callbacks \
  75.         (a b -> a.label = "hum") 1
  76.     cancel humCB  
  77.     
  78.     -- the kettle whistles
  79.     whistle self
  80.     
  81.  
  82.     
  83.     
  84. )
  85.  
  86.  
  87.  
  88. method whistle self {class kettle} ->
  89. (print "Whistle, whistle, whistle !!!!!!!!!!!")
  90.  
  91.  
  92.         
  93. -- Test the kettle simulation
  94. global clock1 := new clock
  95. clock1.rate := 1
  96.  
  97. -- put the kettle on in five seconds to heat 3 cups of water
  98. -- since OnceONly is true, the callback will cancel itself
  99. -- after it is invoked
  100.  
  101. global kettle1 := new Kettle
  102. scheduleKettle kettle1 clock1 3 5 "cb1" true
  103.  
  104. -- schedule the kettle to be put on again in 20 seconds
  105. -- to heat 6 cups of water
  106. -- again, the cancel will clean itself up when it is finished
  107. scheduleKettle kettle1 clock1 6 20 "cb1" true
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. -->>>
  115.